<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" class="custom-dialog" width="80%">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        :label-width="labelWidth"
        v-loading="formLoading"
        class="custom-form"
      >
        <el-form-item label="阶段名称" prop="name" class="form-item">
          <el-input v-model="formData.name" placeholder="请输入阶段名称" class="custom-input" />
        </el-form-item>

        <el-form-item label="排序" prop="sort" class="form-item">
          <el-input-number
            v-model="formData.sort"
            placeholder="请输入排序"
            :min="0"
            :max="100"
            class="custom-input-number"
          />
        </el-form-item>

        <el-form-item label="是否共用" prop="isCommon" class="form-item switch-item">
          <div class="switch-wrapper">
            <span class="switch-label">是否共用:</span>
            <el-switch
              v-model="formData.isCommon"
              placeholder="是否共用"
              :active-value="1"
              :inactive-value="0"
              class="custom-switch"
              style="--el-switch-on-color: #0052cc; --el-switch-off-color: #dcdfe6"
            />
          </div>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="submitForm" type="primary" :disabled="formLoading" class="submit-btn">
          确 定
        </el-button>
        <el-button @click="dialogVisible = false" class="cancel-btn">取 消</el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as FollowStageApi from '@/api/crm/module/index'
const props = defineProps<{
  moduleId: Number
}>()

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  sort: undefined,
  moduleId: props.moduleId,
  isCommon: undefined
})
const formRules = reactive({
  name: [
    {
      required: true,
      message: '阶段名称不能为空',
      trigger: 'blur'
    }
  ],
  sort: [
    {
      required: true,
      message: '排序不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

// 响应式计算属性
const labelWidth = computed(() => {
  return window.innerWidth <= 768 ? '100px' : '110px'
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await FollowStageApi.getFollowStageConfig(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as FollowStageApi.FollowStageVO
    data.moduleId = formData.value.isCommon == 1 ? 0 : props.moduleId

    if (formType.value === 'create') {
      await FollowStageApi.createFollowStageConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await FollowStageApi.updateFollowStageConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    sort: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style scoped>
/* 对话框样式 */
.custom-dialog :deep(.el-dialog) {
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.custom-dialog :deep(.el-dialog__header) {
  background: linear-gradient(135deg, #0052cc 0%, #003d99 100%);
  color: white;
  padding: 20px 24px;
  border-radius: 12px 12px 0 0;
}

.custom-dialog :deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
  font-size: 16px;
}

.custom-dialog :deep(.el-dialog__headerbtn .el-dialog__close) {
  color: white;
  font-size: 18px;
}

.custom-dialog :deep(.el-dialog__headerbtn .el-dialog__close:hover) {
  color: #f0f7ff;
}

.custom-dialog :deep(.el-dialog__body) {
  padding: 24px;
  background: #fafbfc;
}

/* 表单容器 */
.form-container {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.custom-form {
  margin: 0;
}

.form-item {
  margin-bottom: 20px;
}

.form-item :deep(.el-form-item__label) {
  color: #303133;
  font-weight: 600;
  line-height: 1.5;
}

/* 输入框样式 */
.custom-input {
  width: 100%;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

/* 数字输入框样式 */
.custom-input-number {
  width: 100%;
  max-width: 300px;
}

.custom-input-number :deep(.el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

.custom-input-number :deep(.el-input-number__decrease),
.custom-input-number :deep(.el-input-number__increase) {
  color: #0052cc;
}

.custom-input-number :deep(.el-input-number__decrease:hover),
.custom-input-number :deep(.el-input-number__increase:hover) {
  background: #f0f7ff;
  color: #003d99;
}

/* 开关样式 */
.switch-item {
  margin-bottom: 24px;
}

.switch-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
}

.switch-wrapper:hover {
  border-color: #0052cc;
  background: #f0f7ff;
}

.switch-label {
  color: #606266;
  font-weight: 500;
  flex: 1;
}

.custom-switch {
  --el-switch-on-color: #0052cc;
  --el-switch-off-color: #dcdfe6;
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  background: #fafbfc;
  border-radius: 0 0 12px 12px;
  border-top: 1px solid #e4e7ed;
}

.submit-btn {
  padding: 8px 20px;
  border-radius: 6px;
  background: #0052cc;
  border-color: #0052cc;
  font-weight: 600;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background: #003d99;
  border-color: #003d99;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.submit-btn:active {
  transform: translateY(0);
}

.cancel-btn {
  padding: 8px 20px;
  border-radius: 6px;
  background: #f5f7fa;
  border-color: #dcdfe6;
  color: #606266;
  font-weight: 500;
  transition: all 0.3s ease;
}

.cancel-btn:hover {
  background: #ecf5ff;
  border-color: #0052cc;
  color: #0052cc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .custom-dialog :deep(.el-dialog) {
    width: 95% !important;
    margin: 5vh auto;
  }

  .custom-dialog :deep(.el-dialog__header) {
    padding: 16px 20px;
  }

  .custom-dialog :deep(.el-dialog__body) {
    padding: 16px;
  }

  .form-container {
    padding: 16px;
  }

  .form-item {
    margin-bottom: 16px;
  }

  .custom-input-number {
    width: 100%;
    max-width: none;
  }

  .switch-wrapper {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .dialog-footer {
    padding: 12px 16px;
    flex-direction: column;
  }

  .submit-btn,
  .cancel-btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .custom-dialog :deep(.el-dialog) {
    width: 98% !important;
    margin: 2vh auto;
  }

  .custom-dialog :deep(.el-dialog__header) {
    padding: 12px 16px;
  }

  .custom-dialog :deep(.el-dialog__title) {
    font-size: 14px;
  }

  .form-container {
    padding: 12px;
  }
}

/* 加载状态优化 */
.el-loading-mask {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
}

/* 动画效果 */
.form-container {
  transition: all 0.3s ease;
}

.switch-wrapper {
  transition: all 0.3s ease;
}

/* 主题色变量 */
:root {
  --primary-color: #0052cc;
  --primary-hover: #003d99;
  --primary-light: #f0f7ff;
  --border-color: #e4e7ed;
  --text-color: #303133;
  --bg-color: #f8f9fa;
}

/* Element Plus 组件样式覆盖 */
:deep(.el-input__wrapper) {
  border-radius: 6px;
  transition: all 0.3s ease;
}

:deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
